package andengine.tutorial;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.ZoomCamera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.input.touch.detector.ScrollDetector;
import org.anddev.andengine.input.touch.detector.ScrollDetector.IScrollDetectorListener;
import org.anddev.andengine.input.touch.detector.SurfaceScrollDetector;
import org.anddev.andengine.opengl.texture.atlas.ITextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.source.BaseTextureAtlasSource;
import org.anddev.andengine.ui.activity.BaseGameActivity;

public class AndroidTutorialsActivity extends BaseGameActivity implements 
	IScrollDetectorListener, IOnSceneTouchListener {
    
	// constants
	static final int CAMERA_WIDTH=480;
	static final int CAMERA_HEIGHT=320;
	
	static final String TAG="AndroidTutorialsActivity";
	
	// fields
	private ZoomCamera mCamera;
	//private Texture mTexture;
	private TextureRegion mFaceTextureRegion;
	private SurfaceScrollDetector mSurfaceDetector;
	
	private BaseTextureAtlasSource mTextureSource;
	private ITextureAtlas mTextureAtlas;
	
	// ctors
	// get/set
	// overriden methods

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub
		
	}

	/**
	 * {@inheritDoc}<p/>
	 * seems the mEngine will be setted by calling this method automatically
	 */
	@Override
	public Engine onLoadEngine() {
		Engine aEngine=null;
		EngineOptions aOpt=null;
		/*
		 * 1) init the ZoomCamera (the camera denotes the 
		 * portion of the world we can see; simply the screen)
		 * 
		 * b) set bounds
		 */
		this.mCamera=new ZoomCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		//this.mCamera.setBounds(0, CAMERA_WIDTH, 0, CAMERA_HEIGHT*3);
		/*
		 * not sure why... min x, y -> -CAMERA_WIDTH/2, -CAMERA_HEIGHT/2
		 * the max x, y -> CAMERA_WIDTH*3/2, CAMERA_HEIGHT*3/2
		 * the 0,0 origin is based on the center of the screen
		 */
		this.mCamera.setBounds(-CAMERA_WIDTH/2, CAMERA_WIDTH*3/2, -CAMERA_HEIGHT/2, CAMERA_HEIGHT*3/2);
		this.mCamera.setBoundsEnabled(true);
		/*
		 * 2) setup the Engine... (usually 1 activity, needs 1 Engine exactly)
		 */
		aOpt = new EngineOptions(true, ScreenOrientation.LANDSCAPE,
			new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
			this.mCamera);
		aEngine=new Engine(aOpt);
		
		return aEngine;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void onLoadResources() {
		/*
		 * load the texture with a 
		 */
		/*this.mTexture=new BitmapTextureAtlas(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mFaceTextureRegion=TextureRegionFactory.extractFromTexture(this.mTexture, 0, 0, 64, 64, true);
		this.mEngine.getTextureManager().loadTexture(this.mTexture);*/
		
		this.mTextureAtlas=new BitmapTextureAtlas(64, 64);
		this.mTextureSource=new AssetBitmapTextureAtlasSource(
			this, "gfx/ui_ball_1.png", 0, 0);
		this.mFaceTextureRegion=TextureRegionFactory.createFromSource(
			this.mTextureAtlas, this.mTextureSource, 0, 0, true);

		this.mEngine.getTextureManager().loadTexture(this.mTextureAtlas);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Scene onLoadScene() {
		Scene aScene=null;
		int aOriginX=(CAMERA_WIDTH-this.mFaceTextureRegion.getWidth())/2;
		int aOriginY=(CAMERA_HEIGHT-this.mFaceTextureRegion.getHeight())/2;
		Sprite aSprite=null;
		/*
		 *  1) register updateHandler... should be some thread or method instead???
		 */
		this.mEngine.registerUpdateHandler(new FPSLogger());
		/*
		 *  2) setup scene
		 */
		aScene=new Scene();
		aScene.setBackground(new ColorBackground(0, 0, 0.8784f));
		/*
		 * 3) add the Texture to the Scene
		 */
		aSprite=new Sprite(aOriginX, aOriginY, this.mFaceTextureRegion);
		aScene.attachChild(aSprite);
		/*
		 * 4) add the detector for screen scrolling
		 */
		this.mSurfaceDetector=new SurfaceScrollDetector(this);
		this.mSurfaceDetector.setEnabled(true);
		aScene.setOnSceneTouchListener(this);
		aScene.setTouchAreaBindingEnabled(true);
		
		return aScene;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		this.mSurfaceDetector.onTouchEvent(pSceneTouchEvent);
		
		return true;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onScroll(ScrollDetector pScollDetector, TouchEvent arg1, float pDistanceX,
		float pDistanceY) {
		
		//Log.d(TAG, "Scroll {x:"+pDistanceX+", y: "+pDistanceY+"}");
		/*
		 * change offset for the camera's xy co-ordinate
		 */
		this.mCamera.offsetCenter(-pDistanceX, -pDistanceY);
	}
	
	// other methods
	// inner classes (if any)
	
}